package de.hwrberlin.remo.export;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;

/**
 * 
 * This class is used to access all configured model exporters 
 * 
 * @author Mario Sänger (IT 09)
 * @version 1.0
 *
 */
public class ModelExporterRegistry {

	/**
	 * Model exporter extension point id
	 */
	private static final String XP_ID = "de.hwrberlin.remo.model_exporter";
	
	/**
	 * Extension Point fields
	 */
	
	private static final String NAME_ATTRIBUTE 		  = "name";
	private static final String CLASS_ATTRIBUTE 	  = "class";
	
	/**
	 * This method returns all configured model exporters.
	 * 
	 * @return
	 */
	public static List<ModelExporterXP> getRegisteredModelExporters() {
		List<ModelExporterXP> modelExporters = new ArrayList<ModelExporterXP>();
		ModelExporterXP modelExporterXP;
		String name;
		Object modelExporter;
		
		// Get the configuration for the algorithm extension point
		IConfigurationElement[] config = Platform.getExtensionRegistry()
												 .getConfigurationElementsFor(XP_ID);
		
		try {
			for (IConfigurationElement e : config) {
				
				// Get attributes from the configured extensions
				name = e.getAttribute(NAME_ATTRIBUTE);
				modelExporter = e.createExecutableExtension(CLASS_ATTRIBUTE);
				
				// Validate the values of the extension 
				if(name != null && name.length() > 0 && modelExporter instanceof ModelExporter) {
					
					// Build model exporter extension point wrapper
					modelExporterXP = new ModelExporterXP(name, (ModelExporter) modelExporter);
					modelExporters.add(modelExporterXP);
				}
			}
		} catch (CoreException ex) {
			System.out.println(ex.getMessage());
		}
		
		return modelExporters;
	}
}